package com.sanno.maintain.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sanno.common.entity.SccMenu;
import com.sanno.common.result.Result;
import com.sanno.common.result.ResultCodeEnum;
import com.sanno.maintain.dao.Maintain2Dao;
import com.sanno.maintain.entity.*;
import com.sanno.maintain.service.Maintain2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/maintain/SccMenu")
@Api(tags = "系统维护-菜单管理")
@CrossOrigin
public class Maintain2Controller {

    @Resource
    private Maintain2Service maintain2Service;

    @Resource
    private Maintain2Dao maintain2Dao;

    @RequestMapping(value = "findSccMenu",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有",notes = "",httpMethod = "GET")
    public Result querySccMenu(){
        QueryWrapper<SccMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("status",1);
        List<SccMenu> menuList = maintain2Service.list(queryWrapper);
        return Result.ok(menuList);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.POST)
    @ApiOperation(value = "id删除",notes = "",httpMethod = "POST")
    public Result delSccMenu(@PathVariable("id")int id){
        SccMenu sccMenu = new SccMenu();
        sccMenu.setStatus(0);
        UpdateWrapper<SccMenu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("authority_id",id);
        boolean isOk = maintain2Service.update(sccMenu,updateWrapper);
        return Result.build(isOk, ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "addSccMenu",method = RequestMethod.POST)
    @ApiOperation(value = "添加",notes = "",httpMethod = "POST")
    public Result addSccMenu(SccMenu sccMenu){
        boolean idOk = maintain2Service.save(sccMenu);
        return Result.build(idOk,ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "updateSccMenu",method = RequestMethod.POST)
    @ApiOperation(value = "修改",notes = "",httpMethod = "POST")
    public Result updateSccMenu(SccMenu sccMenu){
        System.out.println(sccMenu.getAuthorityId());
        boolean isOk = maintain2Service.updateById(sccMenu);
        return Result.build(isOk,ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "findInit",method = RequestMethod.GET)
    @ApiOperation(value = "Init",notes = "",httpMethod = "GET")
    public Init queryInit(){
        List<Child1> child1List = maintain2Dao.queryInit();
        System.out.println(child1List);
        for (Child1 child1 : child1List) {
            List<Child2> child2List = maintain2Dao.queryChild(child1.getAuthority_id());
            System.out.println(child2List);
            child1.setChild(child2List);
        }
        List<MenuInfo> menuInfoList = new ArrayList<>();
        MenuInfo menuInfo = new MenuInfo();
        menuInfo.setTitle("常规管理");
        menuInfo.setIcon("fa fa-address-book");
        menuInfo.setHref("");
        menuInfo.setChild(child1List);
        menuInfoList.add(menuInfo);
        HomeInfo homeInfo = new HomeInfo();
        homeInfo.setTitle("首页");
        homeInfo.setHref("page/welcome-1.html?t=1");
        Init init = new Init();
        init.setHomeInfo(homeInfo);
        LogoInfo logoInfo = new LogoInfo();
        logoInfo.setTitle("Sanrpid三诺");
        logoInfo.setImage("images/favicon1.ico");
        logoInfo.setHref("");
        init.setLogoInfo(logoInfo);
        init.setMenuInfo(menuInfoList);
        return init;
    }

}
